using System.Diagnostics;
using System.Text.Json;
using CMS.Entity;
using Microsoft.AspNetCore.Mvc;
using CMS.Web.Models;

namespace CMS.Web.Controllers;

public class HomeController : Controller {
    public IActionResult Index() {
        var user = JsonSerializer.Deserialize<User>(HttpContext.Session.GetString("user")!);
        return View(user);
    }

    public IActionResult Privacy() {
        return View();
    }

    [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
    public IActionResult Error() {
        return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
    }

    [HttpPost]
    public IActionResult Logout() {
        HttpContext.Session.Clear();
        HttpContext.Response.Cookies.Delete("uid");
        return Json(new { code = 200 });
    }
}